package com.Yourweibo.util;

import java.io.IOException;
import java.lang.ref.SoftReference;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;

public class ImageLoadAsync {
	private int POLL_SIZE=5;
 
	private String imgUrl;
	private Map<String,SoftReference<Drawable>> ImageCache=new HashMap<String,SoftReference<Drawable>>();
	private ExecutorService executorService = Executors.newFixedThreadPool(5);
	private  Handler handler;
	private ImageCallback callback;
	public Drawable LoadDrawalbe(final String imageUrl,final ImageView imageView,final ImageCallback callback){
		imgUrl=imageUrl;
		//缓存中有图片
		this.callback=callback;
		final Handler handler = new Handler() {
            public void handleMessage(Message message) {
            	callback.imageLoaded((Drawable) message.obj, imageView,imageUrl);
            }
        };
		if(ImageCache.containsKey(imgUrl)){
			SoftReference<Drawable> softReference=ImageCache.get(imgUrl);
			if(softReference.get()!=null){
				System.out.println("SoftReference ImageLoadAsync Loading image!!"+imgUrl);
				return softReference.get();
		
			}
		
			
		}
		//缓存中无图片
		executorService.submit(new Runnable(){

			@Override
			public void run() {
				// TODO Auto-generated method stub
				  Drawable drawable = getImageFromHttp(imageUrl);
	                ImageCache.put(imageUrl, new SoftReference<Drawable>(drawable));
	                Message message = handler.obtainMessage(0, drawable);
	                handler.sendMessage(message);
			}});
	/*	new Runnable(){

			@Override
			public void run() {
				// TODO Auto-generated method stub
				  Drawable drawable = getImageFromHttp(imageUrl);
	                ImageCache.put(imageUrl, new SoftReference<Drawable>(drawable));
	                Message message = handler.obtainMessage(0, drawable);
	                handler.sendMessage(message);
			}};
		*/
	      new Thread() {
	            @Override
	            public void run() {
	                Drawable drawable = getImageFromHttp(imageUrl);
	                ImageCache.put(imageUrl, new SoftReference<Drawable>(drawable));
	                Message message = handler.obtainMessage(0, drawable);
	                handler.sendMessage(message);
	            }
	        }.start();
		//executorService.execute(new getHttpImageTask());
		
		return null;
	}

	public Drawable getImageFromHttp(String imgUrl){
		try {
	 
			return Drawable.createFromStream(new URL(imgUrl).openStream(),
			"image");
		 
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
			
			return null;
		 
		 
	}
	public interface ImageCallback {
		  // 注意 此方法是用来设置目标对象的图像资源
		  public void imageLoaded(Drawable imageDrawable,ImageView imageView, String imageUr);
		 }

}
